function gxdate( SDate, XSFmt)
{
	this.getString = function()
	{
		var sDate = gxdate.SFmt;
		var sDay = gxrpadwc( this.Value.getDate().toString(), 2, '0');
		var sMonth = gxrpadwc( (this.Value.getMonth() + 1).toString(), 2, '0');
		var sYear = gxrpadwc( this.Value.getFullYear().toString(), 4, '0');
		sDate = sDate.replace('D', sDay + '/');
		sDate = sDate.replace('M', sMonth + '/');
		if (SFmt.indexOf("Y4") == -1)
		{
			sYear = sYear.slice( 2,4);
			sDate = sDate.replace('Y', sYear);
		}
		else
			sDate = sDate.replace('Y4', sYear);
		return sDate;		
	}

	this.getUrlVal = function()
	{
		var sDate = gxdate.SFmt;
		var sDay = gxrpadwc( this.Value.getDate().toString(), 2, '0');
		var sMonth = gxrpadwc( (this.Value.getMonth() + 1).toString(), 2, '0');
		var sYear = gxrpadwc( this.Value.getFullYear().toString(), 4, '0');
		var sHour = this.HasTimePart?gxrpadwc(this.Value.getHours().toString(),2,'0'):'';
		var sMin = 	this.HasTimePart?gxrpadwc(this.Value.getMinutes().toString(),2,'0'):'';
		var sSec = this.HasTimePart?gxrpadwc(this.Value.getSeconds().toString(),2,'0'):'';
		return sYear + sMonth + sDay + sHour + sMin + sSec;
	}
	
	this.assign_string = function( SDate, SFmt)
	{
		ANSIDateExp = /([0-9]{1,2})\/?([0-9]{1,2})\/?([0-9]{2,4})\s?([0-9]{2})?:?([0-9]{2})?:?([0-9]{2})?:?([0-9]{2})?\s?(AM|PM)?/i;
		DateParts = ANSIDateExp.exec(SDate);
		if ( SFmt == "ANSI")
		{
			YPos = 3;
			MPos = 2;
			DPos = 1;
		}
		else
		{
			YPos = SFmt.indexOf("Y4") + 1;
			if (YPos == 0)
				YPos = SFmt.indexOf("Y") + 1;
			MPos = SFmt.indexOf("M") + 1;
			DPos = SFmt.indexOf("D") + 1;
		}		
		YY = 0;	MM = 0;	DD = 0;	Ho = 0; Mi = 0; Se = 0; Ce = 0;
		try
		{
			if ((DPos + MPos + YPos == 6) && (DateParts != null) && (DateParts.length >= 3))
			{
				if (DateParts[YPos] != null)
					YY = parseInt( DateParts[YPos], 10);
				if (isNaN( YY)) throw "InvalidDate";
				if (DateParts[MPos] != null)
					MM = parseInt( DateParts[MPos],10) - 1;
				if (isNaN( YY)) throw "InvalidDate";
				if (DateParts[DPos] != null)
					DD = parseInt( DateParts[DPos], 10);
				if (isNaN( DD)) throw "InvalidDate";
				if (YY < 100)
					YY += 2000;
			}
			if ( (DateParts != null) && (DateParts.length > 3))
			{
				//Has time part
				if (DateParts[4] != null)
					Ho = parseInt( DateParts[4], 10);
				if (isNaN( Ho)) throw "InvalidHour";
				if ((SDate.indexOf("PM") != -1 || SDate.indexOf("pm") != -1) && ( Ho < 12))
					Ho += 12;
				if (DateParts[5] != null)
					Mi = parseInt( DateParts[5], 10);
				if (isNaN( Mi)) Mi = 0;
				if (DateParts[6] != null)
					Se = parseInt( DateParts[6], 10);
				if (isNaN( Se)) Se = 0;
				if (DateParts[7] != null)
					Ce = parseInt( DateParts[7], 10);
				if (isNaN( Ce)) Ce = 0;
				this.HasTimePart = true;
			}
			else
				this.HasTimePart = false;
		}
		catch (E)
		{
			if (E == "InvalidDate")
			{
				YY = 0;	MM = 0;	DD = 0;	Ho = 0; Mi = 0; Se = 0; Ce = 0;
			}
			if (E == "InvalidHour")
			{
				Ho = 0; Mi = 0; Se = 0; Ce = 0;
			}
		}
		this.Value = new Date(YY, MM, DD, Ho, Mi, Se, Ce);
	}

	this.compare = function( DateValue)
	{
		if ( typeof(DateValue) == "string")
			return this.compare_string( DateValue);
		return this.compare_date( DateValue);
	}

	this.compare_string = function( SDate)
	{
		DateValue = new gxdate( SDate);
		return this.compare_date( DateValue.Value);
	}
	
	this.compare_date = function( DateValue)
	{
		var Val;
		if ( DateValue instanceof gxdate)
			Val = DateValue.Value;
		else
			Val = DateValue;
		return this.Value - Val;
	}
	var SFmt = XSFmt || gxdate.SFmt;
	this.assign_string( SDate, SFmt);
}

function gxnow()
{
	return new Date();
}


function gxlength( String)
{
	return String.length;	
}

function gxtoupper( String)
{
	return String.toUpperCase();	
}

function gxtolower( String)
{
	return String.toLowerCase();	
}

function gxtoday()
{
	day = new Date();
	day.setHours( 0, 0, 0, 0);
	return day;
}

function gxctot(SDate, SFmt)
{
	return (new gxdate(SDate, SFmt)).Value;
}

function gxctod(SDate, SFmt)
{
	day = new gxdate(SDate, SFmt);
	day.Value.setHours( 0, 0, 0, 0);
	return day.Value;
}

function gxymdtod( Y, M, D)
{
	day = new gxdate(D + '/' + M + '/' + Y, 'ANSI');
	day.Value.setHours( 0, 0, 0, 0);
	return day.Value;
}

function gxymdhmstot( Y, M, D, H, Mi, S)
{
	day = new gxdate(D + '/' + M + '/' + Y + ' ' + H + ':' + Mi + ':' + S, 'ANSI');
	return day.Value;
}


function gxgethour(SDate)
{
	return (new gxdate(SDate)).Value.getHours();
}

function gxgetminute(SDate)
{
	return (new gxdate(SDate)).Value.getMinutes();
}

function gxgetsecond(SDate)
{
	return (new gxdate(SDate)).Value.getSeconds();
}

function gxgetday(SDate)
{
	return (new gxdate(SDate)).Value.getDate();	
}

function gxgetmonth(SDate)
{
	return (new gxdate(SDate)).Value.getMonth() + 1;	
}

function gxgetyear(SDate)
{
	return (new gxdate(SDate)).Value.getFullYear();	
}

function gxsetDateFormat( SFmt)
{
	gxdate.SFmt = new String(SFmt);
}

function gxparseFloat( S, ThSep, DecPoint)
{
	if ( typeof(S) == "number")
		return S;	
	var N = S.replace( ThSep, '');
	N = N.replace( DecPoint, '.');
	return parseFloat( N);	
}

function gxInvalidFunc(StringCode)
{
	throw "gxInvalidFunc: " + StringCode;	
}


function gxrpadwc( val, len, padc)
{
	var xlen = val.length;
	var diff = len - xlen;
	if (diff < 1)
		return val;
	var xval='';
	for (var i=0;i<diff;i++)
		xval += padc;
	xval += val;
	return xval;
}

function gxDateAddDays( sdate, inc)
{
	return gxDateAddMill( sdate, gxDaytoMillisec( inc));
}


function gxDateAddSec( sdate, inc)
{
	return gxDateAddMill( sdate, gxSectoMillisec( inc));
}

function gxDateAddMill( sdate, inc)
{
	var xdate = gxdateparm( sdate);
	date = xdate.Value;
	var mill = date.getMilliseconds();
	date.setMilliseconds( mill + inc);
	return xdate.getString();
}


function gxDateSecDiff( date1, date2)
{
	var val = gxMillisecToSec( gxDateMilliDiff( date1, date2));
	return val;
}

function gxDateDaysDiff( date1, date2)
{
	var val = gxMillisecToDay( gxDateMilliDiff( date1, date2));
	return val;
}

function gxDateMilliDiff( date1, date2)
{
	var xdate1 = gxdateparm( date1);
	var xdate2 = gxdateparm( date2);
	var date1 = xdate1.Value.valueOf();
	var date2 = xdate2.Value.valueOf();
	return date1 - date2;
}

function gxDaytoMillisec( Days)
{
	return Days * 24 * 60 * 60 * 1000;
}

function gxSectoMillisec( Sec)
{
	return Sec * 1000;
}

function gxMillisecToDay( Mill)
{
	return Mill / 24 /60 / 60 / 1000;
}

function gxMillisecToSec( Mill)
{
	return Mill / 1000;
}

function gxdateparm( sdate)
{
	if ( typeof(sdate) == "string")
		return new gxdate( sdate);
	return sdate;	
}

function gxurlvaluedate( Control, sFmt)
{
	var value = typeof( Control) == 'string' ? Control:Control.value;
	var date = new gxdate( value, sFmt);
	return date.Value.getFullYear().toString() 
			+ gxrpadwc((date.Value.getMonth() + 1).toString(),2,'0') 
			+ gxrpadwc(date.Value.getDate().toString(),2,'0');
}

function gxurlvaluedatetime( Control, sFmt)
{
	var value = typeof( Control) == 'string' ? Control:Control.value;
	var date = new gxdate( value, sFmt);
	return 	date.Value.getFullYear().toString() + 
			gxrpadwc((date.Value.getMonth() + 1).toString(),2,'0') + 
			gxrpadwc(date.Value.getDate().toString(),2,'0') +
			gxrpadwc(date.Value.getHours().toString(),2,'0') +
			gxrpadwc(date.Value.getMinutes().toString(),2,'0') +
			gxrpadwc(date.Value.getSeconds().toString(),2,'0');
}

function gxurlvaluedecimal( Control, ThSep, DecPoint)
{
	var value = typeof( Control) == 'string' ? Control:Control.value;
	value = gxparseFloat( value, ThSep, DecPoint);
	return value.toString();
}

function gxurlvalue( Control)
{
	return encodeURIComponent( typeof( Control) == 'string' ? Control:Control.value);
}

function gxltrim( str)
{
	return str.replace(/^ */, '');
}

function gxrtrim( str)
{
	return str.replace(/ *$/, '');
}

function gxtrim( str)
{
	return 	gxrtrim( gxltrim(str));
}
